<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      // index.html
      function jsonp({ url, params, callback }) {
        return new Promise((resolve, reject) => {
          let script = document.createElement("script");
          window[callback] = function (data) {
            resolve(data);
            // document.body.removeChild(script);
          };
          params = { ...params, callback }; // wd=b&callback=show
          let arrs = [];
          for (let key in params) {
            arrs.push(`${key}=${params[key]}`);
          }
          script.src = `${url}?${arrs.join("&")}`;
          document.body.appendChild(script);
        });
      }
      jsonp({
        url: "http://localhost:3000/say",
        params: { wd: "who" },
        callback: "show",
      }).then((data) => {
        console.log(data);
      });
    </script>
  </body>
</html>
